package cn.tedu.csmall.server.controller;

import cn.tedu.csmall.server.exception.ServiceException;
import cn.tedu.csmall.server.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmall.server.pojo.dto.BrandEditDTO;
import cn.tedu.csmall.server.pojo.vo.BrandListItemVO;
import cn.tedu.csmall.server.service.IBrandService;
import cn.tedu.csmall.server.service.impl.BrandService;
import cn.tedu.csmall.server.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author lixin
 * @Date 2022/8/26
 * 品牌管理
 */
//@Controller
@RestController
@RequestMapping("brands")
@Slf4j
@Api(tags = "1.品牌管理模块")
public class BrandController {
    @Autowired
    private IBrandService brandService;

    public BrandController() {
        System.out.println("BrandController.BrandController");
    }

    /**
     * 添加品牌
     *
     * @param brandAddNewDTO 请求传输的数据
     * @return
     */
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    @PostMapping("add-new")
    public JsonResult addNew(@RequestBody @Valid BrandAddNewDTO brandAddNewDTO) {
        log.debug("接收的参数：{}", brandAddNewDTO);
        brandService.addNew(brandAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("修改品牌")
    @PostMapping("/{id}/edit")
    public void updateById(@PathVariable Long id, BrandEditDTO brandEditDTO) {

        System.out.println("id = " + id + ", brandEditDTO = " + brandEditDTO);
    }

    @ApiOperation("删除品牌")
    @ApiOperationSupport(order = 300)
    @GetMapping("/{id:[0-9]+}/delete")
    public JsonResult deleteById(@PathVariable Long id) {
        System.out.println("id = " + id);
        brandService.deleteById(id);
        return JsonResult.ok();
    }

    @GetMapping("/{keywords:[a-zA-Z\\u4e00-\\u9fa5]+}")
    public void listByKeywords(@PathVariable("keywords") String words) {
        System.out.println(words);
    }

    @ApiOperation("品牌列表")
    @ApiOperationSupport(order = 200)
    @GetMapping
    public JsonResult list() {
        final List<BrandListItemVO> list = brandService.list();
        return JsonResult.ok(list);
    }

}
